package com.tridion.utilities.verifier.utils;

import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.tridion.utilities.verifier.model.internal.Root;

public class ReleasesUtil {
    
    private static final String RELEASES_FILE = "/releases.xml";
    
    private static Root LOADED_ROOT = null;
    
    // no use of a real constructor in an utility class
    private ReleasesUtil() {}

    public static Root loadReleases() {
        if (LOADED_ROOT == null) {
            try {
                final InputStream inputStream = ReleasesUtil.class.getResourceAsStream(RELEASES_FILE);
                final JAXBContext context = JAXBContext.newInstance(Root.class);
                final Unmarshaller unmarshaller = context.createUnmarshaller();
                LOADED_ROOT = (Root) unmarshaller.unmarshal(inputStream);
            } catch (JAXBException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        return LOADED_ROOT;
    }
    
    // this is an internal test method
    public static void main(String[] args) {
        loadReleases();
    }
}
